<template>
    <div id="AsnDetailList">
        <v-tableTitle :btnConfig="btnConfig" :tableSelectData="selectData" @backList="backList"
                      @del="del" @newCreate="newCreate"
                      @mirringdetailgenenalnexttask="mirringdetailgenenalnexttask"
                      @createTaskAction="createTaskAction"
                      @queryTaskMoveAction="queryTaskMoveAction"></v-tableTitle>
        <div class="detail">
            <component :data="{detailData,argument}" :is="currentComponent"></component>
        </div>
      <el-dialog :modal="false" :visible.sync="dialogFlag" title="" top="0" v-dialogDrag @close="dialogFlag = false"
                 :close-on-click-modal="false" :append-to-body="true" width="900px">
        <v-tableTitle  :btnConfig="btnConfig2" :title="''"
                       @deletetaskAction="deletetaskAction" @moveloctaskAction="moveloctaskAction"></v-tableTitle>
        <v-table :pageSize="pageSize" :page="page" :tableConfig="tableConfig1" :tableData="tableData" :total="total"
                 @findData="getTableData" @getTableData="getTableData" @searhData="searhData" @selectData="selectDetailData"
                 @upData="getTableData" ref="table"></v-table>
      </el-dialog>
    </div>

</template>

<script>
import tableTitle from "commons/TableTitle";
import navMenu from "commons/NavMenu";
import baseMixin from "@/mixins/BaseMixin";
import config from "../Config"
import table from "commons/Table";

export default {
    name: "DetailList",
    mixins: [baseMixin, config],
    data() {
        return {
          loading: false,
          flag: false,
          dialogTableVisible: false,
            newFlag: false,
            detailsFlag: false,
            argument: {},
            detailData: {},
            selectData: [],
            detailNav: "1",
            componentConfig: [
                {
                    name: "detailTable",
                    label: "",
                    component: () => import('./page/detailTable.vue')
                },
                {
                    name: "tableDetail",
                    label: this.$t('common.detail'),
                    component: () => import('./page/tableDetail.vue')
                },
            ],
          dialogFlag: false,
          lookUrl: "/queryTransferDetailTaskList/",
          pageSize: 10,
          page: 1,
          total: 10,
          tableData: [],
          tableSearhData: {},
          tableUrl: "",
          tableSelectData: [],
        }
    },
    computed: {
        btnConfig() {
            if (this.$store.state.newFlag) {
                return []
            } else if (this.detailNav == 1) {
                return [
                    {label: this.$t('common.create'), certigier: this.newTransferDetailStatus||!this.btnPermNameList.includes('WM06050110'), invalid: "newCreate"},
                    {label: this.$t('common.delete'), certigier: this.newTransferDetailStatus||!this.btnPermNameList.includes('WM06050111'), disabled: true, invalid: "del"},
                    {
                        label: this.$t('work.allsingleTransfer'),
                        certigier: this.newTransferDetailStatus||!this.btnPermNameList.includes('WM06050112'),
                        invalid: "mirringdetailgenenalnexttask"
                    },
                  {
                    label: this.$t('work.createTaskAction'),
                    certigier: this.createTaskStatus||!this.btnPermNameList.includes('WM06050113'),
                    invalid: "createTaskAction"
                  },
                  {
                    label: this.$t('work.queryTaskMoveAction'),
                    certigier: false||!this.btnPermNameList.includes('WM06050114'),
                    invalid: "queryTaskMoveAction"
                  },
                ]
            } else {
                return [
                    {label: this.$t('common.return'), certigier: !this.btnPermNameList.includes('WM06050117'), invalid: "backList"},
                ]
            }
        },
        currentComponent() {
            return this.componentConfig[this.detailNav - 1].component;
        },
      tableConfig1() {
        return [
          {
            label: this.$t('outbound.userlinenumber'),
            prop: "lineno",
            input: "input",
            disabled: false,
          },
          {
            label: this.$t('ex.fromstorerkey'),
            prop: "fshippers",
            input: "input",
            disabled: false,
          },
          {
            label: this.$t('ex.tostorerkey'),
            prop: "tshippers",
            input: "input",
            disabled: false,
          },
          {
            label: this.$t('config.sku'),
            prop: "sku",
            input: "input",
            disabled: false,
          },
          {
            label: this.$t('ex.putawayzone'),
            prop: "putawayzone",
            input: "input",
            disabled: false,
          },
          {
            label: this.$t('work.loc'),
            prop: "loc",
            input: "input",
            disabled: false,
          },
          {
            label: this.$t('in.standardlpn'),
            prop: "lpn",
            disabled: false,
            input: "input",
          },
          {
            label: this.$t('config.num'),
            prop: "qty",
            disabled: false,
            input: "input",
          },
          {
            label: this.$t('common.status'),
            prop: "status",
            input: "select",
            disabled: false,
            options: this.getOptions("codelkup/RTXTRANSFERSTA"),
          }
        ];
      },
      btnConfig2() {
        return [
          {
            label: this.$t('work.moveloctaskAction'), certigier: !this.btnPermNameList.includes('WM06050115'),
            invalid: "moveloctaskAction",
          },
          {
            label: this.$t('common.delete'), certigier: !this.btnPermNameList.includes('WM06050116'),
            invalid: "deletetaskAction",
          },
        ];
      },
    },
    methods: {
        async mirringdetailgenenalnexttask() {
            try {
                let res = await this.$api.get(`/singleTransfer/${this.argument.transferkey}`);
                
                    this.$message.success(res.returnMgr);
                
            } catch (e) {
                this.$message.error(e);
            }
        },
        //删除数据
        async del(data) {
            try {
                let res = await this.$api.post(`/deletedetailAction`, data);
                
                    this.$message.success(res.returnMgr);
                    this.$children[1].getTableData(1, {})
                
            } catch (e) {
                this.$message.error(e);
            } finally {
                this.$children[1].$refs.table.delSelectData(data);
            }
        },
        //选中的数据
        setSelectData(data) {
            this.selectData = data;
        },
        //返回试图
        backList() {
            this.detailsFlag = false;
            this.detailNav = "1";
        },
        //新建
        async newCreate() {
            let data = this.$parent.$refs.detail.detailData
            try {
                let res = await this.$api.get(`/transfer/detail/polinenumber/${this.argument.transferkey}`)
                
                    this.detailData = {
                        transferlineno: res.bean,
                        externalno: data.externalno,
                        fshippers: data.fshippers,
                        tshippers: data.tshippers,
                        uom: 'EA',
                        locqty: '0',
                        lpnqty: '0',
                        qty: '0',
                        status: '0',
                        sku: "",
                        expectedqty: 0
                    }
                    this.detailsFlag = true;
                    this.detailNav = '2'
                
            } catch (e) {
                this.$message.error(e)
            }
        },
        async setdialog(data) {
            try {
                let res = await this.$api.post(`/queryTransferdetailByKey`, {
                    transferkey: this.argument.transferkey,
                    transferlineno: data.transferlineno
                });
                
                    this.detailData = res.bean;
                    this.detailNav = "2";
                
            } catch (e) {
                this.$message.error(e);
            }
        },
      async createTaskAction() {
        try {
          let res = await this.$api.get(`/createTaskForTransfer/${this.argument.transferkey}`);
          
            this.$message.success(res.returnMgr);
            this.dialogFlag = true;
            this.getTableData(this.page, {});
          
        } catch (e) {
          this.$message.error(e);
        }
      },
      async queryTaskMoveAction() {
        this.dialogFlag = true;
        this.getTableData(this.page, {});
      },
      async getTableData(page, data) {
        console.log(page, data);
        try {
          this.loading = true;
          data.transferkey = this.argument.transferkey;
          data.type = '20';
          let url = this.lookUrl  + "/";
          let res = await this.$api.post(`${url}${page}`, data);
          console.log(res);
          
            this.tableData = res.page.list;
            this.page = res.page.curPage;
            this.pageSize = res.page.pageSize;
            this.total = res.page.total;
          
        } catch (e) {
          this.$message.error(e.message);
        } finally {
          this.loading = false;
        }
      },


      //执行转移
      async moveloctaskAction() {
        try {
          let res = await this.$api.get(
              `/moveTransferForKey/${this.argument.transferkey}`
          );
          console.log(res);
          
            this.$message.success(res.returnMgr);
            this.getTableData(this.page, this.tableSearhData);
          
        } catch (error) {
          this.$message.error(error);
        }
      },

      async deletetaskAction() {
        try {
          let res = await this.$api.post(
              `/deletetaskAction`,this.tableSelectData
          );
          console.log(res);
          
            this.$message.success(res.returnMgr);
            this.getTableData(this.page, this.tableSearhData);
            this.$children[1].$children[1].delSelectData(this.tableSelectData);
          
        } catch (error) {
          this.$message.error(error);
        }
      },

      searhData(data, up) {
        if (!up) {
          this.tableSearhData = data;
        } else {
          this.tableSearhData = data;
          this.getTableData(1, this.tableSearhData);
        }
      },

      selectDetailData(data) {
        console.log(data);
        this.tableSelectData = data;
      },
    },
    mounted() {
        this.newFlag = this.$store.state.newFlag;
        if (this.newFlag) {
            let data = this.$parent.$refs.detail.detailData
            this.detailData = {
                transferlineno: "0001",
                externalno: data.externalno,
                fshippers: data.fshippers,
                tshippers: data.tshippers,
                uom: 'EA',
                locqty: '0',
                lpnqty: '0',
                qty: '0',
                status: '0',
                sku: "",
                expectedqty: 0
            }
            this.detailsFlag = true;
            this.detailNav = "2";
        } else if (Object.keys(this.$store.state.skip).length !== 0) {
            let data = this.$store.state.skip;
            this.argument = data.routeData;
            this.detailData = data.detailList;
            this.detailNav = data.detailListNav;
        } else {
            this.argument = this.$store.state.routeData;
        }

    },
    components: {
        "v-tableTitle": tableTitle,
        "v-navMenu": navMenu,
      "v-table": table,
    },
    watch: {
        selectData(data) {
            if (this.permissionStatus) {
                if (data.length > 0) {
                    this.deleteStatus = this.nextTaskStatus = false;
                } else {
                    this.deleteStatus = this.nextTaskStatus = false;
                }
            }
        }
    }
}
</script>

<style>

</style>
